<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="项目方案编号" prop="planNumber">
        <el-input v-model="queryParams.planNumber" placeholder="请输入项目方案编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="项目方案名称" prop="planName">
        <el-input v-model="queryParams.planName" placeholder="请输入项目方案名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="是否保理" prop="factorFlag">
        <el-select v-model="queryParams.factorFlag" placeholder="请选择是否保理" clearable>
          <el-option v-for="dict in dict.type.sys_yes_no" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="审批状态" prop="wflStatus">
        <el-select v-model="queryParams.wflStatus" placeholder="请选择审批状态" value-key="NEW" clearable>
          <el-option v-for="dict in dict.type.sys_approve_status" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini"
                   @click="handleAdd" v-hasPermi="['lease:plan:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"
                   @click="handleUpdate" v-hasPermi="['lease:plan:edit']">修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                   @click="handleDelete" v-hasPermi="['lease:plan:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini"
                   @click="handleExport" v-hasPermi="['lease:plan:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList" @selection-change="handleSelectionChange" border height="calc(100vh - 300px)">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目方案编号" align="center" prop="planNumber" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="项目方案名称" align="center" prop="planName" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="承租人/保理申请人" align="center" prop="bpIdTenant" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="单据类型" align="center" prop="documentType" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="审批状态" align="center" prop="wflStatus" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="提交时间" align="center" prop="submitDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.submitDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="审批通过时间" align="center" prop="approvedDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.approvedDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="单据所有者" align="center" prop="owneUserId" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="集团内行业分类" align="center" prop="industryClassification" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="行业一级分类" align="center" prop="binaryClassification" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="行业二级分类" align="center" prop="secondClassification" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="项目期数" align="center" prop="planTimes" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="项目补增" align="center" prop="planSupplement" width="150" :show-overflow-tooltip="true"/>
      <el-table-column label="是否保理" align="center" prop="factorFlag" width="150" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.breakEvenFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit"
                     @click="handleUpdate(scope.row)" v-hasPermi="['lease:plan:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete"
                     @click="handleDelete(scope.row)" v-hasPermi="['lease:plan:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改项目方案维护对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否保理" prop="factorFlag">
              <el-input v-model="form.factorFlag" placeholder="请输入是否保理" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="承租人/保理申请人" prop="bpIdTenant">
              <el-input v-model="form.bpIdTenant" placeholder="请输入承租人/保理申请人" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单据所有者" prop="owneUserId">
              <el-input v-model="form.owneUserId" placeholder="请输入单据所有者" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="集团内行业分类" prop="industryClassification">
              <el-input v-model="form.industryClassification" placeholder="请输入集团内行业分类" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="行业一级分类" prop="binaryClassification">
              <el-input v-model="form.binaryClassification" placeholder="请输入行业一级分类" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="行业二级分类" prop="secondClassification">
              <el-input v-model="form.secondClassification" placeholder="请输入行业二级分类" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目期数" prop="planTimes">
              <el-input v-model="form.planTimes" placeholder="请输入项目期数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目补增" prop="planSupplement">
              <el-input v-model="form.planSupplement" placeholder="请输入项目补增" />
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlan, getPlan, delPlan, addPlan, updatePlan } from "@/api/lease/plan";

export default {
  name: "Plan",
  dicts: ['sys_yes_no','sys_approve_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目方案维护表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planNumber: null,
        planName: null,
        dataClass: null,
        documentType: null,
        wflInstanceId: null,
        wflStatus: null,
        submitDate: null,
        approvedDate: null,
        owneUserId: null,
        companyId: null,
        cddListId: null,
        templetId: null,
        industryClassification: null,
        binaryClassification: null,
        secondClassification: null,
        planTimes: null,
        planSupplement: null,
        factorFlag: null,
        bpIdTenant: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询项目方案维护列表 */
    getList() {
      this.loading = true;
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planId: null,
        planNumber: null,
        planName: null,
        dataClass: null,
        documentType: null,
        wflInstanceId: null,
        wflStatus: "0",
        submitDate: null,
        approvedDate: null,
        owneUserId: null,
        companyId: null,
        cddListId: null,
        templetId: null,
        industryClassification: null,
        binaryClassification: null,
        secondClassification: null,
        planTimes: null,
        planSupplement: null,
        factorFlag: null,
        bpIdTenant: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.planId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加项目方案维护";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planId = row.planId || this.ids
      getPlan(planId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改项目方案维护";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planId != null) {
            updatePlan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planIds = row.planId || this.ids;
      this.$modal.confirm('是否确认删除项目方案维护编号为"' + planIds + '"的数据项？').then(function() {
        return delPlan(planIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('lease/plan/export', {
        ...this.queryParams
      }, `plan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
